<?php

use yii\bootstrap\ActiveForm;
use yii\helpers\Html;
?>


<div class="row" style="height: 300px;">

    <div class="col-md-offset-3 col-md-6" style="margin-top: 50px;">
        <form id="passwd">
            <input type="hidden" id="_csrf" name="_csrf" value="<?=\Yii::$app->request->getCsrfToken()?>" />

            <div class="form-group">
            <label>新密码</label>
            <?=Html::input('password','password','',['class' => 'form-control']);?>
            </div>
            <div class="form-group">
            <label>确认新密码</label>
            <?=Html::input('password','confimPassword','',['class' => 'form-control']);?>
            </div>

        </form>
        <div class="form-group" style="margin-top:40px;">
        <?= Html::submitButton('确认修改', ['class' => 'btn btn-block btn-success','id'=>'submit']) ?>
        </div>
    </div>

</div>

<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">
$(function(){
   
    $("#submit").click(function(){
   
        var pwd = $.trim($("input[name='password']").val());

        var cpwd = $.trim($("input[name='confimPassword']").val());

        if(pwd.length == 0){
            alert("密码不能为空");
            return false;
        }

        if(pwd != cpwd){
            alert("两次密码不一致!");
            $("input[name='password']").val("");
            $("input[name='confimPassword']").val("");
            return false;
        }
        var _csrf = $("#_csrf").val();
        $.ajax({
           type: "post",
           async:false, 
           url: "/site/save-password",
           data: {'password':pwd,'_csrf':_csrf,},
           success: function(res){
                var res = $.parseJSON(res);
                if(res.code == 0){
                    alert(res.msg);
                    return false;
                }else if(res.code == 1){
                    alert('修改成功,请重新登录');
                    window.location.href = "/site/logout";
                }
           }
        });
    });
});
</script>
<?php app\components\JsBlock::end() ?>


